// MainFrames.cpp : Implementation of CMainFrames

#include "stdafx.h"
#include "MainFrames.h"
#include "MainFrame.h"
#include "ComFramework.h"


STDMETHODIMP CMainFrames::InterfaceSupportsErrorInfo(REFIID riid)
{
	static const IID* arr[] = 
	{
		&IID_IMainFrames
	};

	for (int i=0; i < sizeof(arr) / sizeof(arr[0]); i++)
	{
		if (InlineIsEqualGUID(*arr[i],riid))
			return S_OK;
	}

	return S_FALSE;
}


CMainFrames::CMainFrames():
	m_pActiveMainFrame(NULL)
{
	//theApp->GetMessageLoop().AddIdleHandler(this);
}


//BOOL CMainFrames::OnIdle()
//{
//	MainFramesCol::_ColType::iterator _Iter = this->m_coll.begin();
//	CMainFrame	*_pMainFrame = NULL;
//
//	for (; _Iter != this->m_coll.end(); _Iter++)
//	{
//		_pMainFrame = (*_Iter);
//		if (::IsWindow(_pMainFrame->m_hWnd))
//		{
//			this->m_coll.remove(_pMainFrame);
//			_pMainFrame->Release();
//		}
//	}
//
//	return TRUE;
//}


void CMainFrames::CloseMainFrame(CMainFrame *_Value, safCloseAction *closeAction)
{
	VARIANT_BOOL bClose = VARIANT_TRUE;

	this->Fire_CloseMainFrame((IMainFrames*)this, (IMainFrame*)_Value, &bClose);

	if (bClose)
	{
		m_coll.remove(_Value);
		_Value->DestroyWindow();

		if (m_coll.size() == 0)
		{
			theApp->Quit();
		}
	}
}


STDMETHODIMP CMainFrames::CloseAll(void)
{
	MainFramesCol::_ColType::const_iterator iter = m_coll.begin();
	safCloseAction closeAction = safCloseActionFree;

	for (; iter!=m_coll.end(); iter++)
	{
		CloseMainFrame((*iter), &closeAction);
	}

	return S_OK;
}



STDMETHODIMP CMainFrames::Close(VARIANT varItem)
{
	HRESULT		hr			= E_FAIL;
	IMainFrame	*_pMainFrame = NULL;
	safCloseAction closeAction = safCloseActionFree;

	hr = this->get_Item(varItem, &_pMainFrame);
	if (S_OK == hr)
	{
		CloseMainFrame((CMainFrame*)_pMainFrame, &closeAction);
	}

	return S_OK;
}


//
//STDMETHODIMP CMainFrames::get_Item(VARIANT varItem, IMainFrame** ppMainFrame)
//{
//	
//	return S_OK;
//}
//

STDMETHODIMP CMainFrames::get_ActiveMainFrame(IMainFrame **pActvieMainFrame)
{
	if (NULL != this->m_pActiveMainFrame)
	{
		return this->m_pActiveMainFrame->QueryInterface(IID_IMainFrame, (void**)pActvieMainFrame);
	}

	return E_FAIL;
}


STDMETHODIMP CMainFrames::SetActiveMainFrame(VARIANT varItem)
{

	return S_FALSE;
}
